package com.ctvit.sms.platform.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;

import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.service.BaseService;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.sms.platform.domain.SystemConfig;
import com.ctvit.struts.common.CommonFunc;
import com.ctvit.syn.service.SynchronizeDataService;

public class MsgTaskService extends BaseService {
	private MsgService msgService;
	private SynchronizeDataService synService;
	/**
	 * 检查是否有新的需要发送的短信任务
	 */
	public void checkNewMsgTask()throws Exception{
		try {
			if (StringUtils.equals(CommonFunc.sysid, "local")){
				log.info("begin scan for instock and live msg");
				msgService.doScanMsg();
				log.info("begin create oa notice msg");
				msgService.doGeneNotice();
				synService.synchronizeData();
			}else if (StringUtils.equals(CommonFunc.sysid, "remote")){
				log.info("begin check qualified send task");
				msgService.doExecSendMsg("'1','2'","");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void syncData() throws Exception{
		try {
			if (StringUtils.equals(CommonFunc.sysid, "local")){
				synService.synchronizeData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Calendar getStartTime(){
		Calendar cal = Calendar.getInstance();
//		cal.set(Calendar.AM_PM, Calendar.PM);
//		cal.set(Calendar.HOUR, 12);
//		cal.set(Calendar.MINUTE, 00);
//		cal.set(Calendar.SECOND, 00);
		return cal;
	}
	public void startTimer(){
		Condition condition=new Condition();int intervalSec=120;//默认检查间隔120秒
		if (CommonFunc.intervalSec==0){
			condition.add("zCfgId", "CHECKINTERVAL");//秒数
			SystemConfig sConfig=(SystemConfig)genericDao.getObjectByCondition(SystemConfig.class, condition);
			if (sConfig!=null){
				intervalSec=Integer.parseInt(sConfig.getZCfgVal());
			}
			CommonFunc.intervalSec=intervalSec;
		}else{
			intervalSec=CommonFunc.intervalSec;
		}
		ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
		executorService.scheduleAtFixedRate(new Runnable() {  
            @Override  
            public void run() {  
                try {  
                		checkNewMsgTask();
                    } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        }, 0, intervalSec, TimeUnit.SECONDS); 
//		final Timer impTimer = new Timer();
//		Calendar cal=getStartTime();
//		final Date fstTime = cal.getTime();
//		impTimer.schedule(new java.util.TimerTask() {
//			public void run() {
//				try{
//					checkNewMsgTask();
//				}catch (Exception e){
//					e.printStackTrace();
//				}
//			}
//		}, fstTime, intervalSec*1000);
//		if (StringUtils.equals(CommonFunc.sysid, "local")){
//		ScheduledExecutorService executorService2 = Executors.newScheduledThreadPool(1);
//			executorService2.scheduleAtFixedRate(new Runnable() {  
//	            @Override  
//	            public void run() {  
//	                try {  
//	                		syncData();
//	                    } catch (Exception e) {  
//	                    e.printStackTrace();  
//	                }  
//	            }  
//	        }, 0, intervalSec, TimeUnit.SECONDS); 
//		}
//		if (StringUtils.equals(CommonFunc.sysid, "local")){
//			final Timer syncTimer = new Timer();
//			cal=getStartTime();
//			final Date fstTimeSync = cal.getTime();
//			syncTimer.schedule(new java.util.TimerTask() {
//				public void run() {
//					try{
//						syncData();
//					}catch (Exception e){
//						e.printStackTrace();
//					}
//				}
//			}, fstTimeSync, intervalSec*1000);
//		}
	}
	public MsgService getMsgService() {
		return msgService;
	}
	public void setMsgService(MsgService msgService) {
		this.msgService = msgService;
	}
	public SynchronizeDataService getSynService() {
		return synService;
	}
	public void setSynService(SynchronizeDataService synService) {
		this.synService = synService;
	}
}
